package com.api.shopapi.mapper;

import com.api.shopapi.entity.GoodsClassBusiness;
import com.api.shopapi.entity.dto.GoodsClassBusinessDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface GoodsClassBusinessMapper {
    @Insert("insert into  goods_class_business (" +
            "GoodsClassBusinessCode, " +
            "ParentGoodsClassBusinessCode, " +
            "BusinessCode, " +
            "Name, " +
            "Icon, " +
            "Sort, " +
            "Status " +
            ") " +
            "VALUES " +
            "(" +
            "#{goodsClassBusiness.GoodsClassBusinessCode}," +
            "#{goodsClassBusiness.ParentGoodsClassBusinessCode}," +
            "#{goodsClassBusiness.BusinessCode}," +
            "#{goodsClassBusiness.Name}," +
            "#{goodsClassBusiness.Icon} ," +
            "#{goodsClassBusiness.Sort} ," +
            "#{goodsClassBusiness.Status} " +
            ") ")
    int insert(@Param("goodsClassBusiness") GoodsClassBusiness goodsClassBusiness);


    @Update("update goods_class_business set ParentGoodsClassBusinessCode=#{goodsClassBusiness.ParentGoodsClassBusinessCode}," +
            "Name=#{goodsClassBusiness.Name},Icon=#{goodsClassBusiness.Icon},BusinessCode=#{goodsClassBusiness.BusinessCode}," +
            "Sort=#{goodsClassBusiness.Sort},Status=#{goodsClassBusiness.Status} " +
            "where GoodsClassBusinessCode=#{goodsClassBusiness.GoodsClassBusinessCode}")
    int update(@Param("goodsClassBusiness") GoodsClassBusiness goodsClassBusiness);

    @Delete("delete from goods_class_business where GoodsClassBusinessCode=#{goodsClassBusinessCode}")
    int delete(@Param("goodsClassBusinessCode") String goodsClassBusinessCode);

    @Select("select * from goods_class_business where GoodsClassBusinessCode=#{goodsClassBusinessCode}  limit 1")
    GoodsClassBusiness getByGoodsClassBusinessCode(@Param("goodsClassBusinessCode") String goodsClassBusinessCode);

    @Select("select * from goods_class_business where Name=#{name} and BusinessCode=#{businessCode} and Status=1 limit 1")
    GoodsClassBusiness getByName(@Param("name") String Name,@Param("businessCode") String businessCode);

    @Select("select * from goods_class_business where BusinessCode=#{businessCode} and Status=1 order by Sort desc")
    List<GoodsClassBusiness> getListByBusinessCode(@Param("businessCode") String businessCode);

    @Select("select * from goods_class_business where BusinessCode=#{businessCode} and ParentGoodsClassBusinessCode=#{parentGoodsClassBusinessCode} and Status=1 order by Sort desc")
    List<GoodsClassBusinessDTO> getListByBusinessCodeAndParent(@Param("businessCode") String businessCode, @Param("parentGoodsClassBusinessCode") String parentGoodsClassBusinessCode);

    @Select("select * from goods_class_business" +
            " where BusinessCode=#{businessCode}" +
            " and ParentGoodsClassBusinessCode=#{parentGoodsClassBusinessCode}" +
            " order by Sort desc")
    List<GoodsClassBusinessDTO> getListByParentAll(@Param("businessCode") String businessCode,
                                                   @Param("parentGoodsClassBusinessCode") String parentGoodsClassBusinessCode);

    @Select("<script>" +
            "select * from goods_class_business " +
            "where BusinessCode=#{businessCode} "+
            "<if test ='name !=null and name!=\"\" '>" +
            "and Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='status!=-1'>" +
            "and Status=#{status} " +
            "</if>" +
            "order by Sort desc" +
            "</script>")
    List<GoodsClassBusiness> getPageList(@Param("businessCode") String businessCode,@Param("name") String name,@Param("status")int status);


}
